<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common/include :: header('新增通知公告')"/>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required ">公告标题</label>
        <div class="layui-input-block">
            <input type="text" name="noticeTitle"
                   lay-verify="required" lay-reqtext="公告标题不能为空"
                   placeholder="请输入公告标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required ">公告类型</label>
        <div class="layui-input-block"  style="z-index: 11111;">
            <select name="noticeType" th:with="type=${@dict.getDict('NOTICE_TYPE')}">
                <option value="">请选择</option>
                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label  ">公告内容</label>
        <div class="layui-input-block" id="noticeContent">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label  ">启用状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" lay-skin="switch" lay-text="正常|关闭" >
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<th:block th:include="common/include :: footer"/>

<script th:inline="javascript">
    layui.use(['form', 'laydate','wangEditor'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer,
            wangEditor = layui.wangEditor,
            $ = layui.$;

        var editor = new wangEditor('#noticeContent');
        editor.customConfig.uploadImgServer = ctx + "common/upload";
        editor.customConfig.uploadFileName = 'uploadFile';  // 文件名称
        editor.customConfig.pasteFilterStyle = false;  // 粘贴过滤样式
        editor.customConfig.uploadImgMaxSize = 10 * 1024 * 1024;  // 文件最大5M
        editor.customConfig.uploadImgMaxLength = 1;    // 一次最多上传几个文件
        editor.customConfig.uploadImgHooks = {
            // 上传超时
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
            customInsert: function (insertImg, result, editor) {
                if (result.code == 1) {
                    var url = result.data.url;
                    insertImg(url);
                } else {
                    layer.msg(result.msg);
                }
            }
        };
        editor.customConfig.customAlert = function (info) {
            layer.msg(info);
        };
        editor.create();


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field.status = $("input[name='status']").is(':checked') == true ? 0 : 1;
            data.field.noticeContent = editor.txt.html();
            $.ajax({
                url: ctx + 'sysNotice/insert',
                type: 'POST',
                async: false,
                contentType: 'application/json; charset=UTF-8',
                dataType: "json",
                data: JSON.stringify(data.field),
                success: function (res) {
                    var index = layer.alert(res.msg, {
                        title: '提示信息'
                    }, function () {
                        // 重新加载列表
                        window.parent.location.reload();
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.close(index);
                    if (XMLHttpRequest.status == 404) {
                        window.location.href = "404";
                    } else {
                        layer.msg("服务器好像出了点问题！请稍后试试");
                    }
                }
            });

            return false;
        });

    });
</script>